<?php
/**
 * 餐饮店铺接口
 * @author 孙银川
 * @date   2018.1.25
 */
namespace app\restaurant\controller;

class Shop extends Base{

	/**
     * 获取店铺分类
     */
	public function cates(){
		$data = db('types')->field('ty_id,ty_name,ty_ico')->where("ty_status=1 and ty_module='cy'")->order('ty_sort asc')->select();
		foreach($data as $k=>$v){
			if($v['ty_ico']) $data[$k]['ty_ico'] = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/'.$v['ty_ico'];
		}
		return $this->getOutput(200,'ok',$data);
	}

	/**
     * 获取所有店铺/分类下店铺
     */
	public function shopList(){
		// 分类ID
		$ty_id = $this->request->param('ty_id');
		$map[] = ['sp_module','=','cy'];
		$map[] = ['sp_status','=',1];
		if($ty_id) $map[] = ['sp_tyid','=',$ty_id];
		// 店铺
		$data = db('shops')->field('sp_id,sp_name,sp_logo_url')->where($map)->select();
		foreach($data as $k=>$v){
			if($v['sp_logo_url']) $data[$k]['sp_logo_url'] = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/'.$v['sp_logo_url'];
		}
		return $this->getOutput(200,'ok',$data);
	}

	/**
     * 获取店铺详情
     */
	public function shopDetail(){
		// 店铺ID
		$shop_id = $this->request->param('shop_id');
		if($shop_id){
			$data = db('shops')->field('sp_id,sp_name,sp_phone,sp_logo_url,sp_address,sp_area_id,sp_start_time,sp_end_time')->where('sp_id='.$shop_id)->find();
			if($data['sp_area_id']){
				$area = db('area')->field('merger_name')->where('id='.$data['sp_area_id'])->find();
				$data['area_name'] = str_replace('中国,','',$area['merger_name']);
			}
			if($data['sp_logo_url']) $data['sp_logo_url'] = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/'.$data['sp_logo_url'];
			return $this->getOutput(200,'ok',$data);
		}else{
			return $this->getOutput('400','获取失败');
		}
	}

	/**
     * 获取店铺所有商品
     */
	public function shopGoods(){
		// 店铺ID
		$shop_id = $this->request->param('shop_id');
		if($shop_id){
			$data = db('dc_goods')->field('gs_id,gs_name,gs_price,gs_pic')->where("gs_status=1 and gs_shop_id=".$shop_id)->select();
			foreach($data as $k=>$v){
				if($v['gs_pic']) $data[$k]['gs_pic'] = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/'.$v['gs_pic'];
			}
			return $this->getOutput(200,'ok',$data);
		}else{
			return $this->getOutput(400,'获取失败');
		}
	}

	/**
     * 获取商品详情
     */	
	public function goodsDetail(){
		// 商品ID
		$goods_id = $this->request->param('goods_id');
		if($goods_id){
			$data = db('dc_goods')->where('gs_id='.$goods_id)->find();
			if($data['gs_pic']) $data['gs_pic'] = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/'.$data['gs_pic'];
			return $this->getOutput(200,'ok',$data);
		}else{
			return $this->getOutput(400,'获取失败');
		}
	}
}
?>